<?php
/**
 * @author krku
 */

function smarty_function_insertPhotoCropper($params, &$smarty) {

	$ret = "";

	$width = $smarty->get_template_vars('photoWidth');
	$height = $smarty->get_template_vars('photoHeight');

	$minWidth = $smarty->get_template_vars('minPhotoWidth');
	$minHeight = $smarty->get_template_vars('minPhotoHeight');

	$uploadUrl = Router::GenerateUrl('uploadUrl', $smarty->get_template_vars('uploadUrl'));
	$cutUrl = Router::GenerateUrl('cutUrl',$smarty->get_template_vars('cutUrl'));

	if (isset($params['loadOnFly']) && $params['loadOnFly']) {
		$onFly = true;
		$ret = '
		<iframe src="puste" id="' . $params['prefix'] . 'PhotoUploadIFrame" name="' . $params['prefix'] . 'PhotoUploadIFrame" class="photoUpload" frameborder="0" width="1" height="1"></iframe>
		<div class="photoUploadForm" id="' . $params['prefix'] . 'PhotoUploadForm">
			<form onsubmit="return false;" action="' . $uploadUrl . '" method="post" name="' . $params['prefix'] . 'PhotoUpload" target="' . $params['prefix'] . 'PhotoUploadIFrame" enctype="multipart/form-data" />
				<input type="file" name="photo" onchange="startUpload(\'' . $params['prefix'] . 'PhotoUploadIFrame\', this.form); this.form.submit();" title="" />
				<input name="onFly" value="1" type="hidden" />
				<input name="cropPrefix" value="' . $params['prefix'] . '" type="hidden" />
			</form>
		</div>';
	} else {
		$obFly = false;
	}

	$ret .= '
		<form name="photoCropper" action="' . $cutUrl . '" method="post" onsubmit="doCropPhoto(this, \'photoCropper\'); return false;">
			<div class="cropperMainPhotoFrame">
				<img src="' . ($onFly ? $smarty->get_template_vars('noPhotoImgBig') : '') . '" id="' . $params['prefix'] . 'CropArea" class="PhotoBig" ';

	if ($height != false && $width != false) {
		$ret .= 'style="height: ' . $height . 'px; width: ' . $width . 'px;"';
	}

	if (isset($params['preview'])) {
		if (!is_string($params['preview'])) {
			$preview = $params['prefix'] . "CropPreview";
		} else {
			$preview = $params['preview'];
		}
	}

	$ret .= ' />
			</div>
			<div class="cropperInfoFrame">';

	if (isset($preview)) {
		$ret .= '
				<h4>Podgląd</h4>
				<div id="' . $preview . '" class="' . $params['prefix'] . 'PhotoSmall">
					<img id="' . $params['prefix'] . 'PreviewArea" src="' . ($onFly ? $smarty->get_template_vars('noPhotoImgSmall') : '') . '"></img>
				</div>
				Wybierz odpowiedni kadr przesuwając i regulując krawędzie kwadratowego obszaru.';
	}

	if (isset($params['viewForm']) && $params['viewForm']) {
		$type = "text";
	} else {
		$type = "hidden";
	}
	
	$desc = '';
	if(isset($params['description']) && $params['description'] == true) {
		$desc = '<textarea name="' . $params['prefix'] . 'Description" id="' . $params['prefix'] . 'Description" class="cropper"></textarea>';
	} else {
		$desc = '<input type="hidden" name="' . $params['prefix'] . 'Description" id="' . $params['prefix'] . 'Description" value="0" />';
	}
	
	$ret .= '
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropX1" id="' . $params['prefix'] . 'ImageCropX1" />
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropX2" id="' . $params['prefix'] . 'ImageCropX2" />
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropY1" id="' . $params['prefix'] . 'ImageCropY1" />
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropY2" id="' . $params['prefix'] . 'ImageCropY2" />
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropWidth" id="' . $params['prefix'] . 'ImageCropWidth" />
				<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropHeight" id="' . $params['prefix'] . 'ImageCropHeight" />
				<input type="hidden" name="' . $params['prefix'] . 'ImageFileName" id="' . $params['prefix'] . 'ImageFileName" value="' . $params['photoSrc'] . '"/>
			</div>
			' . $desc . '
			<div class="clearBoth"></div>
			<div class="popoverCancel">
				<input alt="deactivate" class="lbAction button buttonAnuluj" type="button" value="" />
			</div>
			<div class="popoverInfo" id="cropInfo"></div>
			<div class="popoverOk" id="cropButton"></div>
			<div class="clearBoth"></div>
			<script type="text/javascript" language="javascript">'."
				initializeCropper('$preview', '$params[prefix]', $minWidth, $minHeight, $params[onInit], " . ($params['loadOnFly'] ? 'true' : 'false') . ");
			</script>
		</form>";

	return $ret;
}
?>